
package com.example.testutil.util.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOperator {
    /** buffer size in bytes */
    final static int BUFFER_SIZE = 100;

    /**
     * copy file using FileInputStream & FileOutputStream
     * 
     * @param src copy from
     * @param dest copy to
     * @return;
     */
    public static void copyWithFileStream(File src, String srcfilename, File dest, String destfilename) {
        FileInputStream input = null;
        FileOutputStream output = null;

        File msrc = new File(src, srcfilename);
        File mdest = new File(dest, destfilename);
        if (!msrc.exists()) {
            return;
        }
        try {
            if (!dest.exists()) {
                dest.mkdir();
            }
            if (!mdest.exists()) {
                mdest.createNewFile();
            }

            input = new FileInputStream(msrc);
            output = new FileOutputStream(mdest);

            byte[] buffer = new byte[BUFFER_SIZE];
            int copySize;
            while ((copySize = input.read(buffer)) > 0) {
                output.write(buffer, 0, copySize);
                output.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                input.close();
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * copy file using BufferedInputStream & BufferedOutputStream
     * 
     * @param src copy from file
     * @param dest copy to file
     * @return;
     */
    public static void copyWithBufferedStream(File src, String srcfilename, File dest, String destfilename) {

        BufferedInputStream bufferedInput = null;
        BufferedOutputStream bufferedOutput = null;

        File msrc = new File(src, srcfilename);
        File mdest = new File(dest, destfilename);
        if (!msrc.exists()) {
            return;
        }

        try {

            if (!dest.exists()) {
                dest.mkdirs();
            }
            if (!mdest.exists()) {
                mdest.createNewFile();
            }
            bufferedInput = new BufferedInputStream(new FileInputStream(src));
            bufferedOutput = new BufferedOutputStream(new FileOutputStream(dest));

            byte[] buffer = new byte[BUFFER_SIZE];
            int copySize;

            while ((copySize = bufferedInput.read(buffer)) > 0) {
                bufferedOutput.write(buffer, 0, copySize);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedInput.close();
                bufferedOutput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
